<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="轮播图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <j-image-upload v-model="model.url" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isEnable">
              <j-dict-select-tag type="radio" v-model="model.isEnable" dictCode="enable_or_not" placeholder="请选择是否启用" />
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { randomUUID } from '@/utils/util'

  export default {
    name: 'WebsiteImageForm',
    components: {
    },
    props: {
      // 流程表单data
      formData: {
        type: Object,
        default: () => {},
        required: false
      },
      // 表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model: {
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          url: [
            { required: true, message: '请输入轮播图!' }
          ],
          sort: [
            { required: true, message: '请输入排序!' }
          ],
          isEnable: [
            { required: true, message: '请输入是否启用!' }
          ]
        },
        url: {
          add: '/official/toursism/websiteimage/add',
          edit: '/official/toursism/websiteimage/edit',
          queryById: '/official/toursism/websiteimage/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return true
          }
        }
        return false
      }
    },
    created () {
      // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))
      // 如果是流程中表单，则需要加载流程表单data
      this.showFlowData()
    },
    methods: {
      add () {
        this.edit(this.modelDefault)
      },
      edit (record) {
        this.model = Object.assign({}, record)
        this.visible = true
        this.model.fromActionToken = randomUUID()
      },
      // 渲染流程表单数据
      showFlowData() {
        if (this.formBpm === true) {
          let params = { id: this.formData.dataId }
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result)
            }
          })
        }
      },
      submitForm () {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (!valid) {
            that.$emit('cancelConfirmLoading')
            return
          }
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
              that.$emit('cancelConfirmLoading')
              that.model.fromActionToken = randomUUID()
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        })
      }
    }
  }
</script>
